//
//  ZGLAboutUsVC.m
//  Faker
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLAboutUsVC.h"
#import "ZGLAboutFirstTVC.h"
#import "ZGLAboutSecondTVC.h"
#import "ZGLAboutThirdTVC.h"
#import <MessageUI/MessageUI.h>


#pragma mark 1 - 声明属性 签协议
@interface ZGLAboutUsVC ()<UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ZGLAboutUsVC

-(void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftButton)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"关于我们";
    [self creatTableView];
    // Do any additional setup after loading the view.
}
-(void)leftButton {
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 2 - 创建tableView
-(void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ZGLAboutFirstTVC class] forCellReuseIdentifier:@"ZGLAboutUsVC_ZGLAboutFirstTVC"];
    [self.tableView registerClass:[ZGLAboutSecondTVC class] forCellReuseIdentifier:@"ZGLAboutUsVC_ZGLAboutSecondTVC.h"];
    [self.tableView registerClass:[ZGLAboutThirdTVC class] forCellReuseIdentifier:@"ZGLAboutUsVC_ZGLAboutThirdTVC.h"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.section) {
        ZGLAboutFirstTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLAboutUsVC_ZGLAboutFirstTVC"];
        return cell;
        
    }else if (1 == indexPath.section){
        ZGLAboutSecondTVC *cell =[tableView dequeueReusableCellWithIdentifier:@"ZGLAboutUsVC_ZGLAboutSecondTVC.h"];
        [cell.emailButton addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
        return cell;
        
    }else {
        ZGLAboutThirdTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLAboutUsVC_ZGLAboutThirdTVC.h"];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.section) {
        return 260 * ScaleX;
    }else if (1 == indexPath.section){
        return 200 * ScaleX;
    }else {
        return 105 * ScaleX;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (0 == section) {
        return CGFLOAT_MIN;
    }else {
        return 1 * ScaleX;
    }
    
}

-(void)sendMessage {
     [self sendMailInApp];
}

- (void)sendMailInApp
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前系统版本不支持应用内发送邮件功能，您可以使用mailto方法代替" preferredStyle:UIAlertControllerStyleAlert];
        /*添加action*/
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alertController addAction:action1];
        [self presentViewController:alertController animated:YES completion:^{
        }];
        return;
    }
    if (![mailClass canSendMail]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户没有设置邮件账户" preferredStyle:UIAlertControllerStyleAlert];
        /*添加action*/
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alertController addAction:action1];
        
        [self presentViewController:alertController animated:YES completion:^{
        }];
        return;
    }
    [self displayMailPicker];
}

//调出邮件发送窗口
- (void)displayMailPicker
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: @"eMail主题"];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"first@icould.com"];
    [mailPicker setToRecipients: toRecipients];
    NSString *emailBody = @"<font color='red'>eMail</font> 正文";
    [mailPicker setMessageBody:emailBody isHTML:YES];
    [self presentViewController:mailPicker animated:YES completion:^{
    }];
    
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"用户成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"用户点击发送，将邮件放到队列中，还没发送";
            break;
        case MFMailComposeResultFailed:
            msg = @"用户试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    /*添加action*/
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    [alertController addAction:action1];
    
    [self presentViewController:alertController animated:YES completion:^{
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
